﻿// LOGINDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "LOGINDlg.h"
#include "afxdialogex.h"


// LOGINDlg 对话框

IMPLEMENT_DYNAMIC(LOGINDlg, CDialogEx)

LOGINDlg::LOGINDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_LOGIN, pParent)
{

}

LOGINDlg::~LOGINDlg()
{
}

void LOGINDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_EDIT1_Username, Username);
}


BEGIN_MESSAGE_MAP(LOGINDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_Login, &LOGINDlg::OnBnClickedButtonLogin)
	ON_BN_CLICKED(IDC_BUTTON_Logout, &LOGINDlg::OnBnClickedButtonLogout)
	ON_BN_CLICKED(IDC_BUTTON_Passtonew, &LOGINDlg::OnBnClickedButtonPasstonew)
END_MESSAGE_MAP()


// LOGINDlg 消息处理程序


void LOGINDlg::OnBnClickedButtonLogin()
{
	if (gSystemMgr.loginflag == 1) {
		gSystemLog.DisplayLogAndSave(L"请勿重复登陆!", LV_ERROR);
		return;
	}
	CString admin;
	CString password;
	GetDlgItem(IDC_EDIT1_Username)->GetWindowTextW(admin);
	GetDlgItem(IDC_EDIT2_Password)->GetWindowTextW(password);
	if (admin == "admin" && password == gSystemMgr.password)
	{
		gSystemMgr.loginflag = 1;
		gSystemMgr.isAdmin = true;
		AfxMessageBox(L"登陆成功!");
		gSystemLog.DisplayLogAndSave(L"登陆成功!", LV_ERROR);
	}
	else {
		gSystemMgr.isAdmin = false;
		AfxMessageBox(L"用户名或密码错误!");
		gSystemLog.DisplayLogAndSave(L"用户名或密码错误!", LV_ERROR);
	}


}


void LOGINDlg::OnBnClickedButtonLogout()
{
	gSystemMgr.isAdmin = false;
	if (gSystemMgr.loginflag == 0) {
		gSystemLog.DisplayLogAndSave(L"请先登陆!", LV_ERROR);
		return;
	}
	gSystemMgr.loginflag = 0;
	gSystemLog.DisplayLogAndSave(L"用户已登出!", LV_ERROR);
}


void LOGINDlg::OnBnClickedButtonPasstonew()
{
	if (gSystemMgr.loginflag == 0) {
		gSystemLog.DisplayLogAndSave(L"请先登陆!", LV_ERROR);
		return;
	}
	CString password;
	GetDlgItem(IDC_EDIT2_Password)->GetWindowTextW(password);
	gSystemMgr.password = password;
	gSystemMgr.m_cLoginConfigIni.SaveConfigIni(L"LoginConfig", L"m_cPassword", gSystemMgr.password);
	gSystemLog.DisplayLogAndSave(L"密码已修改!", LV_ERROR);
}
